/**
 * Created by Zzhk on 2017/6/30.
 */
$(function () {
    var fn = ABUS.Fn;
    var listData = [];

    document.addEventListener('touchmove', function (e) {
        e.preventDefault();
        isDrag = true;
    }, {passive: false});

    $('#sorting').on('touchend', function (e) {
        var $modal = $('.sorting');
        if ($modal.is(":visible")) {
            $modal.hide();
        }
        else {
            $modal.show();
        }
        e.stopPropagation();
    });
    $('body').on('touchend', function () {
        $('.sorting').hide();
    });

    fn.get({
        url: "/user/order/acceptedChildOrders",
        data: {
            "searchValue": "",
            "orderBy": "",//1业务类型 2剩余时间 3开始时间
            "orderType": ""
        },
        success: function (data) {
            $("#being_process").renderTemp(data);
            listData = data.data;
            new IScroll('#being_process', {mouseWheel: true});
            init(data);
        }
    });

    $("#being_process").on("touchend", "li", function () {
        if (isDrag) {
            isDrag = false;
            return true;
        }
        var data = listData[$(this).index()];
        data.p1 = $(this).data("p1");
        data.p2 = $(this).data("p2");

        if(data.childOrderStatus === 2){
            fn.page_goto({
                url:"./yewuyuan_banli_stopTask.html",
                data:data
            })
        }else {
            fn.page_goto({
                url: "./yewuyuan_banli_startTask.html",
                data: data
            })
        }
    });
    $(".search:first").on("touchend", function () {
        fn.page_goto({
            url: "./yewuyuan_banli_search.html"
        })
    });

    $("#sorting_list").on("touchend", "li", function () {
        fn.get({
            url: "/user/order/acceptedChildOrders",
            data: {
                "searchValue": "",
                "orderBy": $(this).data('value'),//1业务类型 2剩余时间 3开始时间
                "orderType": "ASC"
            },
            success: function (data) {
                $("#being_process").renderTemp(data);
                new IScroll('#being_process', {mouseWheel: true});
                init(data)
            }
        });
    })

    function init(data) {
        var con = $("#being_process");
        con.renderTemp(data);
        new IScroll('#being_process', {mouseWheel: true});

        con.find('li').each(function () {
            var $el = $(this).find('[data-time]');
            if ($el.length > 0 && $el.data('time'))
                fn.countDown($el.data('time'), function (time,min) {
                    $el.text(time)
                })
            var $el1 = $(this).find('[data-time1]');
            if ($el1.length > 0 && $el1.data('time1'))
                fn.countDown($el1.data('time1'), function (time,min) {
                    $el1.text("余:"+min+"分钟")
                })
        })
    }
});